#import "YQOrderRefundShowView.h"

#import "YQOrderRefundVC.h"

#import "YQGoodsHotelOrderDetailsVC.h"
#import "YQGoodsOrderDetailsModel.h"
#import <NSDate+YYAdd.h>
@interface YQGoodsHotelOrderDetailsVC ()<YQOrderManageGoodsCellDelegate>

@end

@implementation YQGoodsHotelOrderDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 1){
        return 3;
    }else if (section == 3){
        return 6;
    }else if (section == 2) {
        return 3;
    }else if (section == 4){
        return 4;
    }
    return 1;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YQOrderManageGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderManageGoodsCell"];
        goodsCell.titleLb.text = self.goodsOrderModel.specification;
        goodsCell.priceLb.text = [NSString stringWithFormat:@"HKD%.2lf",self.goodsOrderModel.goodsPrice];
        goodsCell.countLb.text = [NSString stringWithFormat:@"x%zd",self.goodsOrderModel.goodsNum];
        goodsCell.parameterLb.text = self.goodsOrderModel.standardDesc;
        [goodsCell.goodsIcon setImageWithURL:[NSURL URLWithString:self.goodsOrderModel.goodsImg] placeholder:nil];
        goodsCell.goodsModel = self.goodsOrderModel;
        //控制按钮显示样式
        [goodsCell setHotelRefundStatus:self.goodsOrderModel.refundStatus :NSStringFormat(@"%ld",(long)self.orderDetailsModel.orderStatus)];
        goodsCell.delegate = self;
        return goodsCell;
    }else if (indexPath.section == 1) {
        YQOrderDetailsMoneyCell *moneyCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsMoneyCell"];
        NSString *title = @"";
        NSString *subTitle = @"";
        if (indexPath.row == 0) {
            title = kYQLS(@"商品金额");
            subTitle = [NSString stringWithFormat:@"HKD%@",self.orderDetailsModel.goodAmount];
        }else if (indexPath.row == 1){
            title = kYQLS(@"优惠折扣");
            subTitle = [NSString stringWithFormat:@"-HKD%@",self.orderDetailsModel.couponAmount];
            if (self.orderDetailsModel.couponAmount.intValue == 0) {
                moneyCell.labSubTitle.hidden = YES;
            }else{
                moneyCell.labSubTitle.hidden = NO;
            }
        }else{
            title = kYQLS(@"订单实付款");
            subTitle = [NSString stringWithFormat:@"HKD%@",self.orderDetailsModel.payAmount];
        }
        moneyCell.labSubTitle.text = subTitle;
        moneyCell.labTitle.text = title;
        return moneyCell;
    }else if (indexPath.section == 2) {
        YQOrderDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsInfoCell"];
        NSString *title = @"";
        NSString *subTitle = @"";
        if (indexPath.row == 0) {
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"时间")];
            subTitle = [NSString stringWithFormat:@"%@ - %@",[self timestampSwitchTime:self.orderDetailsModel.startTime.integerValue andFormatter:@"yyyy-MM-dd"],[self timestampSwitchTime:self.orderDetailsModel.endTime.integerValue andFormatter:@"yyyy-MM-dd"]];
        }else if (indexPath.row == 1){
            title = kYQLS(@"使用方式");
            subTitle = self.orderDetailsModel.usageMode;
        }else {
            title = kYQLS(@"酒店地址");
            subTitle = self.orderDetailsModel.address;
        }
        cell.titleLb.text = title;
        cell.infoLb.text = subTitle;
        return cell;
    }else if (indexPath.section == 3) {
        YQOrderDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsInfoCell"];
        NSString *title = @"";
        NSString *subTitle = @"";
        if (indexPath.row == 0) {
            title = [NSString stringWithFormat:@"%@:",kYQLS(@"姓名")];
            subTitle = self.orderDetailsModel.userName;
        }else if (indexPath.row == 3){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"电子邮箱")];
            subTitle = self.orderDetailsModel.userEmail;
        }else if (indexPath.row == 1){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"性别")];
            if (self.orderDetailsModel.userSex == 0 || IsStrEmpty(NSStringFormat(@"%ld",(long)self.orderDetailsModel.userSex))) {
                subTitle = kYQLS(@"女");
            }else{
                subTitle = kYQLS(@"男");
            }
        }else if (indexPath.row == 2){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"出生日期")];
            subTitle = self.orderDetailsModel.userAge;
        }else if (indexPath.row == 5){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"居住地址")];
            subTitle = self.orderDetailsModel.userAddress;
        }else if (indexPath.row == 4){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"联络电话")];
            subTitle = self.orderDetailsModel.userPhone;
        }
        cell.titleLb.text = title;
        cell.infoLb.text = subTitle;
        return cell;
    }else{
        YQOrderDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsInfoCell"];
        NSString *title = @"";
        NSString *subTitle = @"";
        if (indexPath.row == 0) {
            title =[NSString stringWithFormat:@"%@ :",kYQLS(@"订单编号")];
            subTitle = self.orderDetailsModel.orderSn;
        }else if (indexPath.row == 1){
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"订单创建时间")];
            subTitle = [self timestampSwitchTime:self.orderDetailsModel.createTime.integerValue andFormatter:@"yyyy-MM-dd"];//;
        }else if (indexPath.row == 2) {
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"支付时间")];
            subTitle = [self timestampSwitchTime:self.orderDetailsModel.paymentTime.integerValue andFormatter:@"yyyy-MM-dd"];
        }else{
            title = [NSString stringWithFormat:@"%@ :",kYQLS(@"备注")];
            subTitle = self.orderDetailsModel.note;
        }
        cell.titleLb.text = title;
        cell.infoLb.text = subTitle;
        return cell;
    }
}

- (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

#pragma mark - YQOrderManageGoodsCell
//撤销申请
- (void)selectCancelGoodItemModel:(YQGoodsOrderItemModel *)detailModel
{
    [self selectResetGoodItemModel:detailModel];
}
//售后退货退款交互处理
- (void)selectGoodItemModel:(YQGoodsOrderItemModel *)model
{
    [self afterSaleAction:model];
}
//售后
-(void)afterSaleAction:(YQGoodsOrderItemModel *)model{
    
    [YQOrderRefundShowView refundShowViewViewResultBlack:^(NSInteger row) {
        YQOrderRefundVC *refundVC = [[YQOrderRefundVC alloc] init];
        refundVC.type = row;//
        refundVC.OrderModel = model;
        refundVC.detailModel = self.orderDetailsModel;
        ws(self);
        refundVC.ssucceeBlock = ^{
//            [weakself getDetailsData];
        };
        [weakself.navigationController pushViewController:refundVC animated:true];
    }];
}
#pragma mark - 撤销
- (void)selectResetGoodItemModel:(YQGoodsOrderItemModel *)model
{
    NSMutableDictionary *params = NSMutableDictionary.new;
    params[@"refundId"] = @"0";
    params[@"orderItemId"] = model.orderItemId;
    params[@"orderType"] = @(self.orderDetailsModel.orderType);

    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_REFUND_CANCEL_URL parameters:params success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            kMBMesHUD(kYQLS(@"撤销成功"));
            [self getDetailsData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getDetailsData];

}
@end
